<?php
namespace app\api\controller;
// namespace app\zw_admin\controller;
use \think\Controller;
use think\Db;
// use think\Controller;
use think\Session;
use think\Request;
use Endroid\QrCode\Tests;
use think\Paginator;
use qrcode;
use alisms\SmsDemo;
class OrderController extends Controller
{
    /**
     * 预约到店
     * @return [type] [description]
     */
    public function about()
    {
        $selling_id = input('param.selling_id');
        $list['selling_id'] = $selling_id;
        $list['uid'] = input('param.uid');
        $list['time'] = date('Y-m-d');

        $selling_list = db('selling')->where(['selling_id'=>$selling_id])->find();
        $list['selling_name'] = $selling_list['selling_name'];
        // dump($selling_list);
        // exit;
        if (empty($list['selling_id']) || empty($list['uid'])) {
            echo json_encode(array("status"=>3,'message'=>'数据不能为空',"data"=>[]));
            exit;
        }
        if (db('about')->insert($list) > 0) {
            echo json_encode(array("status"=>1,'message'=>'预约成功',"data"=>$selling_list));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'预约失败',"data"=>[]));
            exit;
        }
    }



    /**
     * 判断订单信息
     * @return [type] [description]
     */
    public function if_order()
    {
        // echo '111';
        // exit;
        $uid = input('param.uid');
        $number = input('param.number');
        $arr = [
        	'goods'=>''
        ];
        $where=[
            'number'=>$number
        ];
        $goods_list = Db::name('goods')->where(['number'=>$number,'uid'=>$uid])->select();
        if (empty($goods_list)) {

        	echo json_encode(array("status"=>2,'message'=>'订单信息生成失败!',"data"=>$arr));
            exit;
        }
        $order_id = $goods_list[0]['order_id'];
        $order_list = db('goods_order')->where(['number'=>$number,'order_id'=>$order_id])->select();
        // echo $order_id;
        // dump($goods_list);
        // exit;
        if (!empty($goods_list) && !empty($order_list)) {
            $goods_arr = [];
            $order_arr = [];
            foreach ($order_list as $key => $value) {
                $order_arr[$key]['order_id'] = $value['order_id'];
                $order_arr[$key]['number'] = $value['number'];
                $order_arr[$key]['selling_name'] = $value['selling_name'];
                $order_arr[$key]['member_phone'] = $value['member_phone'];
                $order_arr[$key]['selling_user'] = $value['selling_user'];
                $order_arr[$key]['selling_phone'] = $value['selling_phone'];
                $order_arr[$key]['d_phone'] = $value['d_phone'];
                $order_arr[$key]['user_name'] = $value['user_name'];
                $order_arr[$key]['user_phone'] = $value['user_phone'];
                $order_arr[$key]['time'] = $value['time'];
                $order_arr[$key]['j_time'] = $value['j_time'];
                $order_arr[$key]['user_address'] = $value['user_address'];
                $order_arr[$key]['freight'] = $value['freight'];
                $order_arr[$key]['total'] = number_format($value['total'] + $value['freight'], 2);
                $order_arr[$key]['goods_total'] = number_format($value['total'], 2);
            }
            foreach ($goods_list as $key => $value) {
                $goods_arr['goods'][$key]['goods_name'] = $value['goods_name'];
                $goods_arr['goods'][$key]['com'] = $value['com'];
                $goods_arr['goods'][$key]['num'] = $value['num'];
                $goods_arr['goods'][$key]['brand_name'] = $value['brand_name'];
                $goods_arr['goods'][$key]['unit'] = $value['unit'];
                $goods_arr['goods'][$key]['content'] = $value['content'];
                $goods_arr['goods'][$key]['goods_img'] = "http://www.mochoushop.com/".$value['goods_img'];
                $goods_arr['order'] = $order_arr;
            }
            echo json_encode(array("status"=>1,'message'=>'订单信息生成成功!',"data"=>$goods_arr));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'订单信息生成失败!',"data"=>[]));
            exit;
        }
       
        // // dump($list);
        // exit;
        // $member_list = db('member')->where(['uid'=>$uid])->find();

        // // dump($member_list);
        // $member_phone = $member_list['phone'];


        // $order_list = db('goods')->where(['number'=>$number])->select();
        // // dump($order_list);
        // // exit;
        // $goods_arr = [];
        // $order = [];
        // foreach ($order_list as $key => $value) {
        //     $goods_arr['goods'][$key]['goods_name'] = $value['goods_name'];
        //     $goods_arr['goods'][$key]['com'] = $value['com'];
        //     $goods_arr['goods'][$key]['num'] = $value['num'];
        //     $goods_arr['goods'][$key]['brand_name'] = $value['brand_name'];
        //     $goods_arr['goods'][$key]['unit'] = $value['unit'];
        //     $goods_arr['goods'][$key]['content'] = $value['content'];
        //     $order['order_id'] = $value['order_id'];
        //     $order['number'] = $value['number'];
        //     $order['selling_name'] = $value['selling_name'];
        //     $order['member_phone'] = $value['member_phone'];
        //     $order['selling_user'] = $value['selling_user'];
        //     $order['selling_phone'] = $value['selling_phone'];
        //     $order['d_phone'] = $value['d_phone'];
        //     $order['user_name'] = $value['user_name'];
        //     $order['user_phone'] = $value['user_phone'];
        //     $order['time'] = $value['time'];
        //     $order['j_time'] = $value['j_time'];
        //     $order['user_address'] = $value['user_address'];
        //     $order['freight'] = $value['freight'];
        //     $order['total'] = $value['total'];
        //     // $goods_arr[] = $order;
        // }

        // $goods_arr['order'] = $order;

        // // dump($goods_arr);
        // // exit;
        // if (!empty($order_list)) {
        //     echo json_encode(array("status"=>1,'message'=>'订单信息生成成功!',"data"=>$goods_arr));
        //     exit;
        // }else{
        //     echo json_encode(array("status"=>2,'message'=>'订单信息生成失败!',"data"=>[]));
        //     exit;
        // }
    } 


    /**
     * 确认订单
     * @return [type] [description]
     */
    public function sure_order()
    {
        $uid = input('param.uid');
        $number = input('param.number');
        $where=[
            'number'=>$number,
            'uid'   => $uid
        ];
        $list['type'] = '1';
        $order_list = db('goods_order')->where(['number'=>$number])->find();
        if ($order_list['type'] == '1') {
            echo json_encode(array("status"=>2,'message'=>'请不要重复确认订单!',"data"=>[]));
            exit;
        }
        if (db('goods')->where($where)->update($list) > 0) {
            if (db('goods_order')->where($where)->update($list) > 0) {
                echo json_encode(array("status"=>1,'message'=>'订单确认成功!',"data"=>[]));
                exit;
            }
        }else{
            echo json_encode(array("status"=>2,'message'=>'确认订单失败!',"data"=>[]));
            exit;
        }
        
    }



    /**
     * 订单信息
     * @return [order_type] [返回不同的订单状态]
     * @return [page] [页码]
     */
    public function order_list()
    {
        $order_type = input('param.order_type');
        $uid = input('param.uid');
        $page = input('param.page');
        $size = input('param.size');
        //0:全部订单 / 1:已确认(待付款) / 2:已付款(待收货) / 3:已收货(待评价) / 5:待退款
        if ($order_type == '0') {
            $where = [
                'uid' => $uid,
                'type'=> ['neq','0']
            ];
            $type = '0';
        }else{
            $where = [
                'uid' => $uid,
                'type'=> $order_type
            ];
            $type = $order_type;
        }
       
        $list = [];
        $count = db('goods')->where($where)->order('goods_id desc')->count();
        if ($count == '0') {
            $json['list'] = [];
            echo json_encode(array("status"=>1,'message'=>'暂无订单数据!',"data"=>$json));
            exit;
        }
        if (empty($size)) {
            $list_max_rows = '10';
        } else {
            $list_max_rows = $size;
        }
        $total_page = ceil($count/$list_max_rows);
        $current_page = (int)$page;
        if ($count > 0) {
            $list = db('goods')->where($where)->order('goods_id desc')->page($page,$list_max_rows)->select();

            $number = '';
            foreach ($list as $key => $value) {
                $number .= "`number`="."'".$value['number']."'"." or ";
            }
            $number = rtrim($number,'or ');
            $order_list = db('goods_order')->where($number)->order('order_id desc')->select();

            if ($type == '0') {
                $order_arr = [];
                foreach ($order_list as $key => $value) {
                    if ($value['type'] != $type) {
                        $order_arr[$key] = $value;
                    }
                }
            }else{
                $order_arr = [];
                foreach ($order_list as $key => $value) {
                    if ($value['type'] == $type) {
                        $order_arr[$key] = $value;
                    }
                }
            }
            // dump($order_arr);
            // exit;
            $goods_list = [];
            
            foreach ($list as $k => $v) {
                $v['goods_img'] = "http://www.mochoushop.com/".$v['goods_img'];
                $goods_list[$k] = $v;
                foreach ($order_arr as $key => $value) {
                    if ($v['order_id'] == $value['order_id']) {
                        $goods_list[$k]['selling_name'] = $value['selling_name'];
                        $goods_list[$k]['member_phone'] = $value['member_phone'];
                        $goods_list[$k]['selling_user'] = $value['selling_user'];
                        $goods_list[$k]['d_phone'] = $value['d_phone'];
                        $goods_list[$k]['user_name'] = $value['user_name'];
                        $goods_list[$k]['user_phone'] = $value['user_phone'];
                        $goods_list[$k]['time'] = $value['time'];
                        $goods_list[$k]['j_time'] = $value['j_time'];
                        $goods_list[$k]['user_address'] = $value['user_address'];
                        $goods_list[$k]['freight'] = $value['freight'];
                        $goods_list[$k]['total'] = $value['total'];
                        $goods_list[$k]['type'] = $value['type'];
                        $goods_list[$k]['ali_type'] = $value['ali_type'];
                    }
                }
            }
            
        }

        $goods_res = [];
        $goods_arr_res = [];
        foreach ($goods_list as $k => $v) {
            $goods_res[$v['number']][] = $v;
        }
        foreach ($goods_res as $key => $value) {
            $goods_arr_res[]['goods'] =  $value;
        }

        $result = [
            'count' => $count,
            'list_max_rows' => $list_max_rows,
            'list_rows' => count($list),
            'total_page' => $total_page,
            'current_page' => $current_page,
            'list' => $goods_arr_res
        ];
        echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$result));
        exit;

    }



    /**
     * 支付信息
     * @return [type] [description]
     */
    public function order_pay()
    {
    	// echo '111';
    	// exit;
        $arr = input('param.');

        $account = db('account')->where(['uid'=>$arr['uid']])->find();
        // $user_list['integral'] = $account['integral'];
        $user_list['balance'] = $account['balance'];
        $res = explode(",",$arr['number']);
        $number = '';
        foreach ($res as $key => $value) {
            $number .= "`number`="."'".$value."'"." or ";

        }
        

        $number = rtrim($number,'or ');

        if (empty($number)) {
            echo json_encode(array("status"=>1,'message'=>'订单不可为空!',"data"=>$user_list));
            exit;
        }
        $goods_list = db('goods')->where($number)->select();
       
        $number_to = '';
        foreach ($goods_list as $key => $value) {
            $number_to .= "`number`="."'".$value['number']."'"." or ";
        }
        $number_to = rtrim($number_to,'or ');
        $order_list = db('goods_order')->where($number_to)->select();
        /**
         * 判断支付订单是否存在
         */
        $mergeList = db('merge_order')->select();
        $numList = [];
        $numberArr = [];
        foreach ($mergeList as $key => $value) {
            if ($value['number'] == $arr['number']) {
                $numberArr['num'] = $value['merge_number'];
            }
            // dump($value['number']);
            // $numList = explode(",",$value['number']);
            // foreach ($numList as $k => $v) {
            //     if ($v == $arr['number']) {
            //         $numberArr['num'] = $value['merge_number'];
            //     }
            // }
        }

        // dump($numberArr);
        // exit;
        if (!empty($numberArr)) {
            db('merge_order')->where(['merge_number'=>$numberArr['num']])->delete();
        }
        $count = null;
        foreach ($order_list as $key => $value) {
            // dump($value['total']);
            $count += $value['total'] + $value['freight'];
        }

        // $count = float($count);
        // echo $count;
        // exit;
        
        if($account['balance'] > $count){
           
            $account_list['balance'] = $account['balance'] - $count; //剩余余额
            // $account_list['balance'] = $account['balance'] - $integral; 
            $reduce_balance = $account['balance'] - $account_list['balance']; //扣除余额
            // $reduce_integral =$count - $integral; //扣除的积分
            // $account_list['integral'] = $account['integral'] - $reduce_integral; //剩余的积分;
            $end_total = '0';
        }else{
            $end_total = $count - $account['balance']; //支付金额
            // $account_list['balance'] = $account['balance'] - $integral; 
            $reduce_balance = $count - $end_total; //扣除余额
            $account_list['balance'] = '0'; //剩余余额
        }

        // $end_total = $count - $account['integral'] - $account['balance'];
        // $end_total_integral = $count - $account['integral'];
        // $integral = $count - $end_total_integral;
        // echo $integral.'-----'.$account['integral'].'-------'.$end_total_integral.'--------'.$count;
        // exit;
        
        // dump($reduce_balance);
        // exit;
        // $end_total = number_format($end_total, 2);
        $end_total = sprintf("%.2f",$end_total);
        $reduce_balance = sprintf("%.2f",$reduce_balance);
       
        // echo $end_total;
        // exit;
            
        // echo $end_total; 
        // echo $reduce_balance;
        // exit;
        
        // $end_total = '-16000.00';
        /**
         * 
         * 创建 新的交易订单号
         */
        // echo $end_total;
        // $end_total = abs($end_total);
        if ($end_total < 0) {
            $end_total = 0;
        }
        // $end_total = '6000000.00';
        $merge_number = rand(10000,99999).time();
        $merge_list['merge_number'] = $merge_number; //支付订单号；
        $merge_list['number'] = $arr['number']; //商品订单号
        $merge_list['time'] = date('Y-m-d'); //交易时间
        $merge_list['money'] = $end_total; //支付金额
        $merge_list['uid'] = $arr['uid']; //用户id
        // $merge_list['integral'] = $reduce_integral; //扣除的积分
        $merge_list['balance'] = $reduce_balance; //扣除的余额
       

        $merge_id = db('merge_order')->insertGetId($merge_list);
        $merge_number = db('merge_order')->where(['merge_id'=>$merge_id])->find();
        // dump($merge_number);
        // exit;

        /**
         * 将最终交易金额存入商品订单表
         */
        // $goods_order_total['end_total'] = $end_total;
        // db('goods_order')->where($number)->update($goods_order_total);

        // dump($goods_list);
        // exit;
        

        $goods_arr = [];
        foreach ($goods_list as $k => $v) {
            $v['goods_img'] = "http://www.mochoushop.com/".$v['goods_img'];
            $goods_arr['goods'][$k] = $v;
            foreach ($order_list as $key => $value) {
                if ($v['order_id'] == $value['order_id']) {
                    $goods_arr['goods'][$k]['selling_name'] = $value['selling_name'];
                    $goods_arr['goods'][$k]['member_phone'] = $value['member_phone'];
                    $goods_arr['goods'][$k]['selling_user'] = $value['selling_user'];
                    $goods_arr['goods'][$k]['d_phone'] = $value['d_phone'];
                    $goods_arr['goods'][$k]['user_name'] = $value['user_name'];
                    $goods_arr['goods'][$k]['user_phone'] = $value['user_phone'];
                    $goods_arr['goods'][$k]['time'] = $value['time'];
                    $goods_arr['goods'][$k]['j_time'] = $value['j_time'];
                    $goods_arr['goods'][$k]['user_address'] = $value['user_address'];
                    $goods_arr['goods'][$k]['freight'] = $value['freight'];
                    $goods_arr['goods'][$k]['total'] = $value['total'];
                    $goods_arr['goods'][$k]['type'] = $value['type'];
                    $goods_arr['goods'][$k]['ali_type'] = $value['ali_type'];
                    $goods_arr['money']['end_total'] = $end_total;

                    $goods_arr['money']['balance'] = $reduce_balance;
                    $goods_arr['merge']['merge_number'] = $merge_number['merge_number'];
                }
            }
        }
        // dump($goods_arr);
        // exit;
        
        if (!empty($goods_arr)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$goods_arr));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
        }
        // echo $count;
        // exit;
        // $goods_arr = [];
        // $order = [];
        // foreach ($order_list as $key => $value) {
        //     $goods_arr['goods'][$key]['goods_name'] = $value['goods_name'];
        //     $goods_arr['goods'][$key]['com'] = $value['com'];
        //     $goods_arr['goods'][$key]['num'] = $value['num'];
        //     $goods_arr['goods'][$key]['brand_name'] = $value['brand_name'];
        //     $goods_arr['goods'][$key]['unit'] = $value['unit'];
        //     $goods_arr['goods'][$key]['content'] = $value['content'];
        //     $order[$key]['order_id'] = $value['order_id'];
        //     $order[$key]['number'] = $value['number'];
        //     $order[$key]['selling_name'] = $value['selling_name'];
        //     $order[$key]['member_phone'] = $value['member_phone'];
        //     $order[$key]['selling_user'] = $value['selling_user'];
        //     $order[$key]['selling_phone'] = $value['selling_phone'];
        //     $order[$key]['d_phone'] = $value['d_phone'];
        //     $order[$key]['user_name'] = $value['user_name'];
        //     $order[$key]['user_phone'] = $value['user_phone'];
        //     $order[$key]['time'] = $value['time'];

        //     $order[$key]['j_time'] = $value['j_time'];
        //     $order[$key]['user_address'] = $value['user_address'];
        //     $order[$key]['freight'] = $value['freight'];
        //     $order[$key]['total'] = $value['total'];
        //     // $order['balance'] = $ulist['balance'];
        //     // $order['integral'] = $ulist['integral'];
        //     // $goods_arr[] = $order;
        // }
       // dump($goods_arr);
       //  // $goods_arr['order'] = $order;
        
       //  // foreach ($order as $key => $value) {
       //  //         // dump($value);
       //  //         if ($value['order_id']) {
       //  //             # code...
       //  //         }
       //  // }

       //  // $order_lisr = array_unique($order);
       //  // dump($res);
       //  // echo $number;
       //  exit;
        // $uid = input('param.uid');
        // $number = input('param.number');
        // $ulist = db('account')->where(['uid'=>$uid])->find();
        // $phone = $ulist['phone'];
        // $order_list = db('goods_order')->where(['number'=>$number,'member_phone'=>$phone])->select();
        // // dump($ulist);
        // // dump($order_list);
        // // exit;
        // // $member_order = [];
        // $goods_arr = [];
        // $order = [];
        // foreach ($order_list as $key => $value) {
        //     $goods_arr['goods'][$key]['goods_name'] = $value['goods_name'];
        //     $goods_arr['goods'][$key]['com'] = $value['com'];
        //     $goods_arr['goods'][$key]['num'] = $value['num'];
        //     $goods_arr['goods'][$key]['brand_name'] = $value['brand_name'];
        //     $goods_arr['goods'][$key]['unit'] = $value['unit'];
        //     $goods_arr['goods'][$key]['content'] = $value['content'];
        //     $order['order_id'] = $value['order_id'];
        //     $order['number'] = $value['number'];
        //     $order['selling_name'] = $value['selling_name'];
        //     $order['member_phone'] = $value['member_phone'];
        //     $order['selling_user'] = $value['selling_user'];
        //     $order['selling_phone'] = $value['selling_phone'];
        //     $order['d_phone'] = $value['d_phone'];
        //     $order['user_name'] = $value['user_name'];
        //     $order['user_phone'] = $value['user_phone'];
        //     $order['time'] = $value['time'];
        //     $order['j_time'] = $value['j_time'];
        //     $order['user_address'] = $value['user_address'];
        //     $order['freight'] = $value['freight'];
        //     $order['total'] = $value['total'];
        //     $order['balance'] = $ulist['balance'];
        //     $order['integral'] = $ulist['integral'];
        //     // $goods_arr[] = $order;
        // }
        // $goods_arr['order'] = $order;
        // $total =  $goods_arr['order']['total'] + $goods_arr['order']['freight'];
        // $end_total = $total - $goods_arr['order']['balance'] - $goods_arr['order']['integral'];
        // $goods_arr['order']['end_total'] = $end_total;
        // if (!empty($goods_arr)) {
        //     echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$goods_arr));
        //     exit;
        // }else{
        //     echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
        //     exit;
        // }
        // echo $end_total;
        // dump($goods_arr);
        // $total = $goods_arr

    }



    /**
     * 确认收货详情
     * @return [type] [description]
     */
    public function collect_index()
    {
        $number = input('param.number');
        $uid = input('param.uid');
        // 
        /**
         * 获取交易订单信息
         */
        $mergeList = db('merge_order')->select();
        $numList = [];
        $numberRes = [];
        foreach ($mergeList as $key => $value) {
            $numList = explode(",",$value['number']);
            foreach ($numList as $k => $v) {
                if ($v == $number) {
                    $numberRes['num'] = $value['merge_number'];
                }
            }
        }
        $balanceList = db('merge_order')->where(['merge_number'=>$numberRes['num']])->find();
        // dump($balanceList);
        // exit;
        $goods_order = db('goods_order')->where(['number'=>$number,'uid'=>$uid])->find();
        
        $goods_list = db('goods')->where(['number'=>$number,'uid'=>$uid])->select();
        $list = [];
        foreach ($goods_list as $key => $value) {
            $value['goods_img'] = "http://www.mochoushop.com/".$value['goods_img'];
            $list['goods'][$key] =$value; 
            $list['order'] = $goods_order;
            $list['order']['balance'] = $balanceList['balance'];
        }
        // dump($list);
        // exit;
        if (!empty($list)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$list));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
        }

    }


    /**
     * 确认收货
     * @return [type] [description]
     */
    public function collect_goods()
    {
    	$number = input('param.number');
    	$uid = input('param.uid');
    	$type = input('param.type');
    	if ($type != '3') {
    		echo json_encode(array("status"=>1,'message'=>'参数错误!',"data"=>[]));
       		exit;
    	}
    	$edit['type'] = '3';

    	if (db('goods_order')->where(['number'=>$number,'uid'=>$uid])->update($edit) > 0 ) {
    		db('goods')->where(['number'=>$number,'uid'=>$uid])->update($edit);
    		echo json_encode(array("status"=>1,'message'=>'确认收货成功!',"data"=>[]));
       		exit;
    	}else{
    		echo json_encode(array("status"=>2,'message'=>'确认收货失败!',"data"=>[]));
       		exit;
    	}



    } 
    



    /**
     * 订单评价
     * @return [uid] [用户id]
     * @return [number] [订单编号]
     * @return [content] [评价内容]
     * @return [goods_value] [商品满意度]
     * @return [ambient_value] [环境满意度]
     * @return [serve_value] [服务满意度]
     */
    public function order_assess()
    {
        $list = input('param.');
        if (db('order_assess')->insert($list) > 0) {
            $type['type'] = '4';
            db('goods')->where(['number'=>$list['number']])->update($type);
            db('goods_order')->where(['number'=>$list['number']])->update($type);
            echo json_encode(array("status"=>1,'message'=>'评价成功!',"data"=>[]));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'评价失败!',"data"=>[]));
            exit;
        }

    }






    /**
     * 退换/货
     * @return [gid] [商品id]
     * @return [quit_type] [服务类型]
     * @return [reason] [申请原因]
     * @return [content] [问题描述]
     * @return [number] [商品订单号]
     * @return [uid] [用户Id]
     */
    public function quit_order()
    {
        $list = input('param.');
        $list['time'] = date('Y-m-d H:i');
        $type['type'] = '5';
        $goodsList = Db::name('goods')
        ->alias('a')
        ->join('drm_goods_order b','b.number = a.number')
        ->where(['goods_id'=>$list['gid']])
        ->find();
        /**
         * 获取交易订单信息
         */
        $mergeList = db('merge_order')->where(['ali_type'=>'1'])->select();
        $numList = [];
        $numberTow = [];
        foreach ($mergeList as $key => $value) {
            $numList = explode(",",$value['number']);
            foreach ($numList as $k => $v) {
                if ($v == $goodsList['number']) {
                    $numberTow = $value['merge_number'];
                }
            }
        }
        $numUnit = $goodsList['unit'] * $goodsList['num'];
        /**
         * 查询交易记录
         */
        $recordList = db('record')->where(['merge_number'=>$numberTow])->find();
        // dump($goodsList);
        // exit;
        switch ($goodsList['pay_type']) {
            case '1': //支付宝支付
                $list['ali_unit'] = $numUnit;
                $list['balance_unit'] = '0';
                $list['type'] = $goodsList['pay_type'];
                break;
            case '3': //余额支付
                $list['balance_unit'] = $numUnit;
                $list['ali_unit'] = '0';
                $list['type'] = $goodsList['pay_type'];
                break;
            case '4': //支付宝 + 余额
                $goodsRefund = db('goods_refund')->where(['merge_number'=>$numberTow])->find();
                // dump($number);
                // exit;
                //判断是否部分商品已退款
                if (empty($goodsRefund)) {
                    //如果商品金额大于支付支付金额
                    if ($numUnit >= $recordList['ali_unit']) {
                        //获取余额退款金额
                        $balance_unit = $numUnit - $recordList['ali_unit'];
                        $list['balance_unit'] = sprintf("%.2f",$balance_unit);
                        //获取支付宝退款金额
                        $ali_unit = $numUnit - $list['balance_unit'];
                        $list['ali_unit'] = sprintf("%.2f",$ali_unit);
                    }else{
                    //如果支付宝支付金额大于商品金额
                        //获取支付宝退款金额
                        $ali_count = $recordList['ali_unit'] - $numUnit;
                        $ali_unit = $recordList['ali_unit'] - $ali_count;
                        $list['ali_unit'] = sprintf("%.2f",$ali_unit);
                        //余额退款金额
                        $list['balance_unit'] = '0';
                        
                    }
                }else{ 
                    if ($goodsRefund['ali_unit'] >= $recordList['ali_unit']) {
                        //获取支付宝退款金额
                        $list['ali_unit'] = '0';
                        //余额退款金额
                        $list['balance_unit'] = $numUnit;
                    }else{
                        //获取支付宝退款金额
                        $ali_count = $recordList['ali_unit'] - $goodsRefund['ali_unit'];
                        $balance_unit = $numUnit - $ali_count;
                        $list['balance_unit'] = sprintf("%.2f",$balance_unit);
                        //获取支付宝退款金额
                        $ali_unit = $numUnit - $balance_unit;
                        $list['ali_unit'] = sprintf("%.2f",$ali_unit);
                        
                    }
                }
                // $list['balance_unit'] = $numUnit;
                $list['type'] = $goodsList['pay_type'];
                break;
        }
        db('goods')->where(['goods_id'=>$list['gid']])->update($type);
        /**
         * 保存退款记录
         */

        $refund['gid'] = $list['gid'];
        $refund['refund_number'] = rand(10000,99999).time();
        $refund['merge_number'] = $numberTow;
        $refund['number'] = $list['number'];
        $refund['ali_unit'] = $list['ali_unit'];
        $refund['balance_unit'] = $list['balance_unit'];
        $refund['pay_type'] = $goodsList['pay_type'];
        $refund['uid'] = $list['uid'];
        $refund['time'] = date('Y-m-d H:i');
        // dump($refund);
        // exit;
        db('goods_refund')->insert($refund);
        if (db('quit_goods')->insert($list) > 0) {
            echo json_encode(array("status"=>1,'message'=>'申请成功!',"data"=>[]));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'申请失败!',"data"=>[]));
            exit;
        }
    }









    /**
     * 退换货详情
     *@return [gid] [商品id]
     *@return [uid] [用户Id]
     */
    public function refund_index()
    {
        $gid = input('param.gid');
        $uid = input('param.uid');
        $quitGoodsList = db('quit_goods')->where(['gid'=>$gid,'uid'=>$uid])->select();
        dump($quitGoodsList);
        exit;
        foreach ($quitGoodsList as $key => $value) {
            $value['total'] = $value['ali_unit'] + $value['wx_unit'] + $value['balance_unit'];
            $list[] = $value; 
        }
        // dump($list);
        // exit;
        if (!empty($quitGoodsList)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$quitGoodsList));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
        }
    }








    
}

